package com.example.cloud.controller.business.medical;

import com.alibaba.fastjson.JSON;
import com.example.cloud.constants.StateConstants;
import com.example.cloud.controller.common.BaseController;
import com.example.cloud.model.business.medical.TreatPlanHomeProject;
import com.example.cloud.model.dto.QueryModel;
import com.example.cloud.model.dto.Result;
import com.example.cloud.service.business.medical.TreatPlanHomeProjectService;
import com.example.cloud.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by wangtao on 2018/7/2.
 */
@RestController
@RequestMapping("/treat/plan/home/project")
public class TreatPlanHomeProjectController extends BaseController {

    @Autowired
    private TreatPlanHomeProjectService treatPlanHomeProjectService;

    @PostMapping("/search")
    public Result search(@RequestBody QueryModel model) {
        return this.singleQuery(model, TreatPlanHomeProject.class);
    }

    @PostMapping("/save")
    public void save(@RequestBody TreatPlanHomeProject record) {
        treatPlanHomeProjectService.doSave(record);
    }

    @PostMapping("/add")
    public void add(@RequestBody List<TreatPlanHomeProject> records){
        treatPlanHomeProjectService.doAdd(records);
    }

    @PostMapping("/execute")
    public Result execute(@RequestBody Map map) {
        Long id = Long.valueOf(map.get("id").toString());
        int status = Integer.parseInt(map.get("status").toString());
        String content = map.get("content").toString();
        TreatPlanHomeProject record = treatPlanHomeProjectService.findOne(id);
        if(status == StateConstants.EXECUTE_STATUS_YES){
            if (record.getExecute() >= record.getQuantity()) {
                return ResultUtil.fail("治疗已结束");
            }
        }
        treatPlanHomeProjectService.doExecute(record, status, content);
        return ResultUtil.success("执行成功");
    }

    /**
     * 停止
     */
    @PostMapping("/stop/{id}")
    public void stop(@PathVariable Long id) {
        treatPlanHomeProjectService.doStop(id);
    }

    /**
     * 置换
     */
    @PostMapping("/replace")
    public void replace(@RequestBody Map map) {
        treatPlanHomeProjectService.doReplace(JSON.parseObject(map.get("d1").toString(), TreatPlanHomeProject.class), JSON.parseArray(map.get("d2").toString(), TreatPlanHomeProject.class));
    }

    /**
     * 删除
     */
    @PostMapping("/delete/{id}")
    public void delete(@PathVariable Long id) {
        treatPlanHomeProjectService.doDelete(id);
    }
}
